package com.bjpowernode.pmes.web.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;

import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.bjpowernode.pmes.bean.Menu;
import com.bjpowernode.pmes.bean.Page;
import com.bjpowernode.pmes.bean.Result;
import com.bjpowernode.pmes.bean.User;
import com.bjpowernode.pmes.bean.Users;
import com.bjpowernode.pmes.service.UserService;
import com.bjpowernode.pmes.util.Const;
import com.bjpowernode.pmes.util.DateUtil;

@Controller
@RequestMapping("/user")
public class UserController {

	@Resource
	private UserService userService;
	
	@Resource
	private JavaMailSender sendMail;
	
	@Resource
	private SimpleMailMessage mailConfig;
	
	/**
	 * 查询用户的菜单数据
	 * @return
	 * @throws Exception
	 */
	@ResponseBody
	@RequestMapping("/loadMenu1")
	public Object loadMenu1( HttpSession session ) throws Exception {
		
		// 获取当前登录用户的角色信息
		User loginUser = (User)session.getAttribute("loginUser");
		String role = loginUser.getRole();
		
		//List<Menu> rootMenuList = userService.getMenus(0);
		List<Menu> rootMenuList = new ArrayList<Menu>();
		
		// 查询所有的菜单数据
		List<Menu> menuList = userService.getRoleMenus(role);
		
		Map<Integer, Menu> menuMap = new HashMap<Integer, Menu>();
		
		for ( Menu menu : menuList ) {
			menuMap.put(menu.getId(), menu);
			if ( menu.getPid() == 0 ) {
				rootMenuList.add(menu);
			}
		}
		
		// 组合父菜单和子菜单之间的关系
		for ( Menu menu : menuList ) {
			// 子菜单
			Menu childMenu = menu;
			
			// 获取子菜单的父菜单ID
			Integer pid = childMenu.getPid();

			// 查找父菜单
			Menu parentMenu = menuMap.get(pid);
			if ( parentMenu != null ) {
				parentMenu.getChildren().add(childMenu);
			}
			
		}
		
		return rootMenuList;
	}
	
	@ResponseBody
	@RequestMapping("/loadMenu")
	public Object loadMenu() throws Exception {
		
		//List<Menu> rootMenuList = userService.getMenus(0);
		List<Menu> rootMenuList = new ArrayList<Menu>();
		
		// 查询所有的菜单数据
		List<Menu> menuList = userService.getAllMenus();
		
		// 组合父菜单和子菜单之间的关系
		for ( Menu menu : menuList ) {
			// 子菜单
			Menu childMenu = menu;
			
			// 获取子菜单的父菜单ID
			Integer pid = childMenu.getPid();
			
			if ( pid == 0 ) {
				rootMenuList.add(menu);
			}
			
			// 查找父菜单
			for ( Menu innerMenu : menuList ) {
				
				if ( innerMenu.getId() == pid ) {
					// 父菜单
					Menu parentMenu = innerMenu;
					// 组合父菜单和子菜单之间的关系
					parentMenu.getChildren().add(childMenu);
				}
			}
		}
		
		return rootMenuList;
	}
	
	/**
	 * 用户登录功能
	 * @return
	 * @throws Exception
	 */
	@ResponseBody
	@RequestMapping("/doLogin")
	public Object doLogin( User user, HttpSession session ) throws Exception {
		Result r = new Result();
		
		try {
			User loginUser = userService.getUserForLogin(user);
			if ( loginUser != null ) {
				//model.addAttribute("loginUser", loginUser);
				// 将登录后的用户信息保存在session当中。
				session.setAttribute(Const.SESS_P_USER, loginUser);
			}
			r.setSuccess(loginUser != null);
		} catch ( Exception e ) {
			e.printStackTrace();
			r.setSuccess(false);
		}
		
		return r;
	}
	
	/**
	 * 删除选择的用户信息
	 * @return
	 * @throws Exception
	 */
	@ResponseBody
	@RequestMapping("/deleteUsers")
	public Object deleteUsers( Users users ) throws Exception {
		Result r = new Result();
		
		try {
			List<User> userList = users.getUsers();
			int count = userService.deleteUsers(userList);
			r.setSuccess(count == userList.size());
		} catch ( Exception e ) {
			e.printStackTrace();
			r.setSuccess(false);
		}
		
		return r;
	}
	
	/**
	 * 修改用户信息
	 * @return
	 * @throws Exception
	 */
	@ResponseBody
	@RequestMapping("/updateUser")
	public Object updateUser( User user ) throws Exception {
		
		Result r = new Result();
		
		try {
			int count = userService.updateUser(user);
			r.setSuccess(count == 1);
		} catch ( Exception e ) {
			e.printStackTrace();
			r.setSuccess(false);
		}
		
		return r;
		
	}
	
	/**
	 * 跳转到修改页面
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/edit")
	public String edit( Integer id, Model model ) throws Exception {
		
		// 查询数据
		User user = userService.getUserById(id);
		
		// 将查询结果保存在模型中
		model.addAttribute("user", user);
		
		// 跳转页面
		return "user/edit";
	}
	
	/**
	 * 新增用户信息
	 * @return
	 * @throws Exception
	 */
	@ResponseBody
	@RequestMapping("/insertUser")
	public Object insertUser( User user ) throws Exception {
		
		Result r = new Result();
		
		try {
			// 保存时间
			user.setCreatetime(DateUtil.getSystemtime());
			// 保存用户信息
			int count = userService.insertUser(user);
			
		    // 发送注册邮件
			//mailConfig.setTo(user.getEmail());
			//mailConfig.setText("恭喜您，"+user.getUsername()+", PMES系统帐号注册成功。\n此邮件为系统邮件，无需回复。");
			//sendMail.send(mailConfig);
			
			r.setSuccess(count == 1);
		} catch ( Exception e ) {
			e.printStackTrace();
			r.setSuccess(false);
		}
		
		return r;

	}
	
	/**
	 * 跳转到用户的新增页面
	 * @return
	 */
	@RequestMapping("/add")
	public String add() {
		return "user/add";
		// null;
	}
	
	/**
	 * 分页查询用户信息
	 * @return
	 * @throws Exception
	 */
	@ResponseBody
	@RequestMapping("/pageQuery")
	public Object pageQuery( @RequestParam("page")Integer pageno, @RequestParam("rows")Integer pagesize ) throws Exception {
		
		Page<User> page = userService.getAllUsers(pageno, pagesize);
		
		Map<String, Object> resultMap = new HashMap<String, Object>();
		
		resultMap.put("total", page.getTotalsize());
		resultMap.put("rows", page.getDataList());
		
		return resultMap;
	}
	
	/**
	 * 跳转到用户的分页查询页面
	 * @return
	 */
	@RequestMapping("/index")
	public String index() {
		
//		if ( pageno == null ) {
//		    pageno = 1;	
//		}
//		
//		if ( pagesize == null ) {
//			pagesize = 5;
//		}
//		
//		// 分页查询数据
//		Page<User> page = userService.getAllUsers(pageno, pagesize);
//		
//		model.addAttribute("page", page);
		
		return "user/index";
		// null;
	}
	
	
}
